<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['weiriya:order:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['weiriya:order:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['weiriya:order:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" fixed="left" align="center" />
      <el-table-column label="#" type="index" fixed="left" width="80" align="center">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单编号"
        align="center"
        prop="sn"
        fixed="left"
        width="260"
      />
      <el-table-column
        label="用户昵称"
        align="center"
        prop="memberName"
        min-width="120"
      />
      <el-table-column label="商品类型" align="center" width="120">
        <template slot-scope="scope">
          {{ goodsType(scope.row.goodsType) }}
        </template>
      </el-table-column>
      <el-table-column
        label="商品金额(元)"
        align="center"
        prop="goodsPrice"
        width="120"
      />
      <el-table-column
        label="实付金额(元)"
        align="center"
        prop="finalPrice"
        width="120"
      />
      <el-table-column label="订单类型" align="center" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.memberType == 2 ? 'warning' : 'success'"
            effect="plain"
          >
            {{ scope.row.memberType == 2 ? "临时订单" : "会员订单" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.orderStatus == 'PAID'
                ? 'success'
                : scope.row.orderStatus == 'UNPAID'
                ? 'warning'
                : 'danger'
            "
            effect="plain"
          >
            {{
              scope.row.orderStatus == "PAID"
                ? "已支付"
                : scope.row.orderStatus == "UNPAID"
                ? "未支付"
                : "已取消"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="订单取消原因"
        align="center"
        prop="cancelReason"
        min-width="120"
      />
      <el-table-column label="停车点位" align="center" prop="areaName" width="180" />
      <el-table-column label="创建类型" align="center" width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.payType == 1 ? 'success' : 'warning'" effect="plain">
            {{ scope.row.payType == 1 ? "用户创建" : "后台创建" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completeTime" width="180" />
      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180" />
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.orderStatus == 'UNPAID'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['weiriya:order:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['weiriya:order:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="修改订单价格"
      :visible.sync="open"
      :close-on-click-modal="false"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="sn">
          {{ form.sn }}
        </el-form-item>
        <el-form-item label="应付价格" prop="goodsPrice">
          {{ form.goodsPrice || "-" }} 元
        </el-form-item>
        <el-form-item label="实付价格" prop="finalPrice">
          <el-input v-model="form.finalPrice" placeholder="请输入实付价格" clearable>
            <template slot="suffix">元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/weiriya/order";

export default {
  name: "Order",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: false, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      orderList: [], // 用户支付订单表格数据
      title: "", // 弹出层标题
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sn: null,
        memberName: null,
      },
      form: {}, // 表单参数
      // 表单校验
      rules: {
        finalPrice: [
          { required: true, message: "请输入实付价格", trigger: "blur" },
          { pattern: /^\d+(\.\d{1,2})?$/, message: "请输入正确的价格", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value > this.form.goodsPrice) {
                callback(new Error("实付价格不能大于应付价格"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询用户支付订单列表
    getList() {
      this.loading = true;
      listOrder(this.queryParams)
        .then((res) => {
          this.orderList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 获取商品类型名称
    goodsType(val) {
      let list = [
        { label: "充电月卡", value: "MONTH_CHARGE" },
        { label: "充电季卡", value: "QUARTER_CHARGE" },
        { label: "充电年卡", value: "YEAR_CHARGE" },
        { label: "停车月卡", value: "MONTH_DEPOSIT" },
        { label: "停车季卡", value: "QUARTER_DEPOSIT" },
        { label: "停车年卡", value: "YEAR_DEPOSIT" },
        { label: "余额充值", value: "WALLET_RECHARGE" },
      ];
      let item = list.find((i) => i.value == val);
      return item ? item.label : val;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sn: null,
        goodsType: null,
        goodsPrice: null,
        memberType: null,
        orderStatus: null,
        cancelReason: null,
        payOrderNo: null,
        paymentTime: null,
        completeTime: null,
      };
      this.resetForm("form");
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 修改
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = "修改用户支付订单";
      });
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateOrder(this.form).then((res) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户支付订单编号为"' + ids + '"的数据项？')
        .then(() => {
          delOrder(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
    // 导出
    handleExport() {
      this.download(
        "weiriya/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
